package db

import (
	"context"
	"data_summary/common/config"
	"fmt"
	"github.com/go-redis/redis/v8"
	"time"
)

var rdb *redis.Client //创建redis连接池

func init() {
	var host1 config.Host
	host1.GetHost("hosts", "redis")
	rdb = redis.NewClient(&redis.Options{
		Addr:         host1.Url,
		Password:     host1.Password,
		DB:           0,
		DialTimeout:  10 * time.Second, // 连接超时时间
		ReadTimeout:  30 * time.Second, // 读取超时时间
		WriteTimeout: 30 * time.Second, // 写入超时时间
		PoolSize:     10,               // 连接池大小
	})
	ctx := context.TODO()
	if err := rdb.Ping(ctx).Err(); err != nil {
		fmt.Print("reids 连接错误", err)
		panic("连接错误")
	}
	//defer rdb.Close()
}

func GetPool() *redis.Client {
	return rdb
}
